<?php
namespace app\zb\controller;
use think\Controller;
use think\Db;
use think\Exception;
use think\Loader;
use think\Session;
use think\View;

class Callback extends Controller
{
    /**
     * stdClass Object转array
     * */
    function object2array(&$object) {
        $object =  json_decode( json_encode( $object),true);
        return  $object;
    }
    /*
     * 获取上传视频信息
     * */
    function getPlayInfo($videoId) {
        Loader::import('AliyunOpenapiPhpSdkMaster.aliyun-php-sdk-core.Config');
        Loader::import('AliyunOpenapiPhpSdkMaster.aliyun-php-sdk-core.DefaultAcsClient');
        $regionId = 'cn-shanghai';
        $profile = \DefaultProfile::getProfile($regionId, 'LTAI4JFTraQjomTV', 'jXR9FpGrrnJt08YHdnzaOmhDiG2Nj8');
        $client = new \DefaultAcsClient($profile);
        $request = new \vod\Request\V20170321\GetPlayInfoRequest();

        $request->setVideoId($videoId); // 准备播放的视频ID
        $request->setFormats("mp4,m3u8");
        $request->setAuthTimeout("1800");

        $response = $client->getAcsResponse($request);

        $info = $this->object2array($response);

        return $info;
    }
    /**
     * 截取某二个特殊字符串间的某段字符串
     * @str   原字符串
     * @str1   第一个字符串在源字符串中出现的位置
     * @str2   第二个字符串在源字符串中出现的位置
     * */
    public function getNeedBetween($str,$str1,$str2)
    {
        $i = strrpos($str, $str1);
        $j = strrpos($str, $str2);
        $b = substr($str, $i + strlen($str1), $j - $i - strlen($str1));//截取字符串，其中$i+strlen($str1)计算起始位置，$j-$i-strlen($str1)计算截取字符串的长度
        return $b;
    }
    /**
     * 视频转码完成的回调地址
     * 视频信息入库
     * */
    public function TranscodingCallBack()
    {
        header("Content-type: text/html; charset=utf-8");

        $data = input('param.');

        file_put_contents(RUNTIME_PATH . '/TranscodingCallBack.txt', json_encode($data));
        try {
            if($data['Status'] == 'success'){
                $videoInfo = $this->getPlayInfo($data['VideoId']);
                file_put_contents(RUNTIME_PATH.'/video_info.txt',json_encode($videoInfo));
                $info['name'] = $videoInfo['VideoBase']['Title'];//视频标题

                $list = $data['StreamInfos'];
                foreach($list as $k => $v) {

                    $IDcard = $this->getNeedBetween($info['name'], '|-', '-|');//上传视频的用户证件号码
                    if ($IDcard !== false) {
                        //湘女秀异地上传视频
                        $videoData = array('videoId' => $data['VideoId'], 'IDcard' => $IDcard);
                    } else {
                        $videoData = array('VideoId' => $data['VideoId']);
                    }
                    file_put_contents(RUNTIME_PATH.'video_data.txt',json_encode($videoData));
                    /**
                     * 通过视频标题判断该视频上传来源
                     * */
                    if (strpos($info['name'], '湘女秀异地报名视频') !== false) {

                        //湘女秀异地报名
                        if ($v['Format'] == 'mp4' && $v['Definition'] == 'SD') {
                            /*$video_url = $v['FileUrl'];
                            $db = Db::connect('mysql://xsfr_db:xsfr_db_pwd@114.55.75.188:3306/xsfr#utf8');

                            $data = $db->table('wp_show')->where(['IDcard' => $IDcard])->find();

                            if ($data) {
                                //修改
                                $db->table('wp_show')->where('IDcard', $IDcard)->update(['video_url' => '|' . $video_url, 'type' => 2]);
                            } else {
                                //新增
                                $data['video_url'] = $video_url;
                                $data['IDcard'] = $IDcard;
                                $data['type'] = 2;
                                $db->table('wp_show')->insert($data);
                            }*/

                            post_data("http://hnxsfr.mgtvshop.com/index/show/wl_TranscodingCallBack", $videoData);exit;
                        }
                    }

                    if(strpos($info['name'],'最美夫人(xsfr.mgtvshop)') !== false){
                        if($v['Format'] == 'm3u8' && $v['Definition'] == 'SD'){
                            //最美夫人
                            post_data("http://xsfr.mgtvshop.com/index.php?s=/w0/Home/Index/TranscodingCallBack", $videoData);exit;
                        }
                    }else{
                        if($v['Format'] == 'mp4' && $v['Definition'] == 'SD'){
                            //就行天下
                            post_data("http://jx.mgtvshop.com/zb/upload/TranscodingCallBack", $videoData);exit;
                        }
                    }
                }
            }
        } catch (Exception $e) {
            file_put_contents(RUNTIME_PATH . '/TranscodingCallBackError.txt', $e->getMessage());
        }
    }
}